/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

package lime2;
import java.net.*;
import java.io.Serializable;

/**
 * The class modelling the ID of a LimeServer.
 * 
 * @author Gian Pietro Picco
 * @author Amy Murphy
 * @author Lorenzo Bellini
 */

public class LimeServerID implements HashID, Serializable
{	
 private InetAddress host = null;
 private int port = -1;

 /**
  * Constructor method.
  * <br>
  * We change loopback address ("localhost" or "127.0.0.1") into the local address of this host. The reason is that
  * loopback address is considered different from the local address when the computer is connected to a network (see
  * below) but we want them to be equal (.. just in case an user tried to pass loopback address). 
  * <P><code>
  * local = InetAddress.getLocalHost()
  * <br>
  * InetAddress.getByName("this computer").equals(local) <b>= true</b>
  * <br>
  * InetAddress.getByName("localhost").equals(local) <b>= false</b>
  * <br>
  * InetAddress.getByName("127.0.0.1").equals(local) <b>= false</b>
  * <br>
  * local.isLoopbackAddress() <b>= false</b>
  * <br>
  * InetAddress.getByName("localhost").isLoopbackAddress() <b>= true</b>
  * <br>
  * InetAddress.getByName("127.0.0.1").isLoopbackAddress() <b>= true</b>
  * </code></P>
  * When the computer is <i>not</i> connected to a network all the previous equalities are true. Please pay attention
  * to this behavior if you wish to use OS-SW emulated multicasting.
  * 
  * @param host InetAddress of the host
  * @param port port number
  */
 public LimeServerID(InetAddress host, int port) 
 {
  // changes on the fly loopback address into local address	(.. just in case an user tried to pass loopback address) 
  if (host.isLoopbackAddress()) host = LimeServer.getServer().getUnicastAddress(); 
  this.host = host;
  this.port = port;
 }
 
 /**
  * Returns the host.
  * 
  * @return the InetAddress of the LimeServer
  */
 public InetAddress getHost() 
 {
  return host;
 }

 /**
  * Returns the port.
  * 
  * @return the port
  */
 public int getPort() 
 {
  return port;
 }
 
 /**
  * Is it a multicast LimeServerID ?
  * 
  * @return boolean <code>true</code>/<code>false</code>
  */
 public boolean isMulticastID()
 {
  return host.isMulticastAddress();
 }
 
 /**
  * Returns the hashcode.
  * 
  * @return the hashcode
  */ 
 public int hashCode() 
 {
  return (new Integer(host.hashCode()).toString() + new Integer(port).toString()).hashCode();
 }

 /**
  * Tests for equality.
  * 
  * @param o Object
  * @return <code>true</code> if the objects match, <code>false</code> otherwise 
  */ 
 public boolean equals(Object o) 
 {	
  boolean result = false;
  if ((o != null) && (o instanceof LimeServerID))
     {
  	  LimeServerID lsid = (LimeServerID) o;
      result = (host.equals(lsid.host)) && (port == lsid.port);
     }
  return result;
 }

 /**
  * Returns a concise textual description for this object.
  * 
  * @return String
  */
 public String toString()
 {
  String hostName = null;
  if (host.isMulticastAddress()) hostName = host.getHostAddress(); // since a multicast address has no name
  else hostName = host.getHostName();
  return (hostName == null ? "null" : hostName) + ":" + port;
 }
 
}


